<?php
namespace XtwsBack\Controller;
use Think\Controller;
class StoreController extends Controller{
//    店铺列表
    public function index(){
        $member = M('member');
        $store=M('shop_store');
        $community = M('shop_community');
		$where="";
		if($_GET['keyword']!=''){
			$where="storename like '%".$_GET['keyword']."%'";
			$this->assign('keyword',$_GET['keyword']);// 赋值分页输出
		}
        $count = $store->count();// 查询满足要求的总记录数
        $Page = new \Think\Page($count,100);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show = $Page->show();// 分页显示输出
        $store_arr =$store->where($where)->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
        foreach ($store_arr as $k => $v){
            $username = $member->where('id='.$v['u_id'])->field('nickname')->find();
            $store_arr[$k]['username'] = $v['u_id'] == 0 ? "未绑定":$username['nickname'];
            $communityname = $community->where('id='.$v['c_id'])->field('title')->find();
            $store_arr[$k]['title'] = $v['c_id'] == 0 ? "未绑定":$communityname['title'];
//            $store_arr[$k]['credit3'] =  $member->where('id='.$v['u_id'])->getField('credit3');     //店主卡额
        }
//        dump($store_arr);
        $this->assign('list',$store_arr);// 赋值数据集
        $this->assign('page',$show);// 赋值分页输出
        $this->display();
    }//end fun index
//    添加店铺
    public function AddStore(){
        $store=M('shop_store');
        if($_POST){
            $_POST['addtime'] = time();
            $addstore = $store->add($_POST);
            $savemember = M("member")->where(" id = {$_POST['u_id']}")->save(['u_type'=>1]);
            if ($addstore && $savemember){
                $this->success('添加成功！','index.html');
                return;
            }else{
                $this->error('添加失败');
            }
        }
        $this->display();
    }//end fun add
//    编辑店铺
    public function SaveStore()
    {
        $store = M('shop_store');
        $pay = M('shop_pay');
        $member = M('member');
        $community = M('shop_community');
        $community_info = $community->field('id,title')->select();//查询所有社区
        $member_info = $member->where('u_type = 0 AND status = 0')->field('id,nickname')->select();    //查询所有会员信息
        $id = $_GET['id'] ? $_GET['id'] : $_POST['id'];
        $postarr = I("post.");
        if ($id) {
            $store_info = $store->where("id = {$id}")->find();
            //添加默认用户信息
            if ($store_info['u_id'] !=0){
                $mymember = getMember($store_info['u_id']);
                $member_info[]=array(
                    'id' => $mymember['id'],
                    'nickname' => $mymember['nickname']
                );
            }
            //添加默认小区信息
            if ($store_info['c_id'] != 0){
                $mycommunity = $community->where("id =".$store_info['c_id'])->find();
                $community_info[]=array(
                    'id' => $mycommunity['id'],
                    'title' => $mycommunity['title']
                );
            }
            $img_url = $pay->where("u_id = {$store_info['u_id']} AND s_id = {$id}")->field('qrcode')->find();    //查询绑定的二维码
//            var_dump($img_url);
            if ($postarr) {
                $is_img = $pay->where("u_id = {$postarr['u_id']} AND s_id = {$id}")->field('qrcode')->find();    //查询绑定的二维码
                foreach ($postarr as $k => $v) {
                    if ($v == '') {
                        if ($k != 'content') {
                            unset($postarr[$k]);
                        }
                    }
                    foreach ($store_info as $key => $value) {
                        if ($store_info[$k] == $v) {
                            unset($postarr[$k]);
                        }
                    }
                }
//                var_dump($postarr);die;
                if ($postarr) {
                    //生成二维码
                    if (!$is_img && $postarr['u_id'] != 0) {
                        $model = D("Qrcode");
                        $qrcode = $model->Qrcode($id, $postarr['u_id']);
                        $code = "WHS" . $id . $postarr['u_id'];
                        $pay_info = array(
                            'u_id' => $postarr['u_id'],
                            's_id' => $id,
                            'code' => $code,
                            'qrcode' => $qrcode,
                            'addtime' => time()
                        );
                        $addpay = $pay->add($pay_info);
                        if (!$addpay) {
                            $this->error('二维码保存失败');
                        }
                    }
                    if ($postarr['u_id']){
                        M('member')->where("id = {$store_info['u_id']}")->save(['u_type' => 0]);//解除已绑定用户
                        M('member')->where("id = {$postarr['u_id']}")->save(['u_type' => 1]);//绑定新用户
                    }
                    $savestore = $store->where("id = {$id}")->save($postarr);
                    if ($savestore) {
                        $this->success('商品保存成功', 'index.html');
                        return;
                    } else {
                        $this->error('商品保存失败');
                    }
                } else {
                    //生成二维码
                    if (!$is_img && $store_info['u_id'] != 0) {
                        $model = D("Qrcode");
                        $qrcode = $model->Qrcode($id, $store_info['u_id']);
                        $code = "WHS" . $id . $store_info['u_id'];
                        $pay_info = array(
                            'u_id' => $store_info['u_id'],
                            's_id' => $id,
                            'code' => $code,
                            'qrcode' => $qrcode,
                            'addtime' => time()
                        );
                        $addpay = $pay->add($pay_info);
                        if ($addpay) {
                            $this->success('生成二维码成功', 'index.html');
                            return;
                        } else {
                            $this->error('二维码保存失败');
                        }
                    } else {
                        $this->error('商品无修改', 'index.html');
                    }//end if $is_img 是否存在
                }//end if $postarr 是否为空
            }//end if $_POST 是否存在
            $this->assign('img', $img_url['qrcode']);
            $this->assign('store', $store_info);
            $this->assign('community', $community_info);
            $this->assign('member', $member_info);
            $this->display();
        }else{
            $this->error('参数错误');
        }//end if id
    }//end fun save
//    删除店铺
    public function DeleteStore(){
        $store=M('shop_store');
        if ($_GET['id']){
            $storeid = $_GET['id'];
            $delete = $store->where("id = {$storeid}")->save(['status' => 0]);
            $u_id = $store->where("id = {$storeid}")->getField('u_id');
            $member = M('member')->where("id = {$u_id}")->save(['u_type' => 0]);
            if ($delete && $member){
                $this->success('删除成功！','index.html');
                return;
            }else{
                $this->error('删除失败');
            }
//            $this->display();
        }
    }//end fun delete
}//end class